﻿
var _getUserPageUrl = '/User/GetUserPage';
var _addUserUrl = '/User/AddUser';
var _deleteUserUrl = '/User/DeleteUser';
var _loadUserUrl = '/User/LoadUser';
var _saveEditUserUrl = '/User/EditUser';

var _deleteUserId = -1;

function GetUserPage(page, count, orderCol) {
    _currentPage = page;
    var data = {};
    data.page = page;
    data.count = count;
    data.orderColumns = orderCol;

    $('#overlay-loading').show();

    PostServerData(_getUserPageUrl, data, GetUserPageCallback);
}

function GetUserPageCallback(data) {
    if (data.result) {
        $('#user-list-data').html(data.data);
        $('#user-pages').html(data.paging);
        $('#user-count').html("Tổng: " + data.count);
    } else {
        showMessageBox(data.message, "Lỗi tải dữ liệu");
    }
    $('#overlay-loading').hide();
}

function LoadUserInformation(id) {
    //
}

function LoadUserInformationCallBack(data) {
    //
}

// Add user
function AddUser() {
    var user = {};
    user.Email = $('#add_email').val();
    user.FirstName = $("#add_firstname").val();
    user.LastName = $("#add_lastname").val();
    user.Phone = $("#add_phone").val();
    user.Address = $("#add_address").val();
    user.Password = SHA256($("#add_password").val());
    user.RoleId = $("#add_role").val();
    user.Status = $("#add_status").val();

    PostServerData(_addUserUrl, user, AddUserCallBack);

    return false;
}

function AddUserCallBack(data) {
    if (data.result) {
        showMessageBox("Thêm người dùng thành công.", "Thông báo");
        //$('#frm_add_user')[0].reset();
    } else {
        showMessageBox("Có lỗi trong quá trình xử lý. <br />" + data.message, "Lỗi xử lý");
    }
}

// Add user form
function SetupAddUserForm() {
    $('#frm-create-user').dialog({
        autoOpen: false,
        width: 320,
        modal: true,
        draggable: false,
        resizable: false,
        close: function () {
            //
        }
    });
    $('#btn_add_submit, #btn_add_reset').button();
    $('#btn_add_cancel').button().click(function () {
        $('#frm-create-user').dialog('close');
    });
}

function OpenAddUserForm() {
    _isAddNewUser = true;
    $('#frm-create-user').dialog("open");
}

function OpenEditUserForm() {
    _isAddNewUser = false;
    $('#frm-create-user').dialog("open");
}

// Confirm box
function SetupDeleteAllConfirm() {
    $('#dialog-confirm-delete-all').dialog({
        resizable: false,
        draggable : false,
        modal: true,
        autoOpen: false,
        resizable: false,
        buttons: {
            "Đồng ý xóa": function () {
                $(this).dialog("close");
            },
            "Hủy": function () {
                $(this).dialog("close");
            }
        }
    });
}

function ShowConfirmDeleteAll() {
    $('#dialog-confirm-delete-all').dialog("open");
}

function SetupDeleteConfirm() {
    $('#dialog-confirm-delete').dialog({
        resizable: false,
        draggable: false,
        modal: true,
        autoOpen: false,
        resizable: false,
        buttons: {
            "Đồng ý xóa": function () {
                DeleteUser(_deleteUserId);
            },
            "Hủy": function () {
                $(this).dialog("close");
            }
        }
    });
}

function ShowConfirmDelete(id) {
    _deleteUserId = id;
    $('#dialog-confirm-delete').dialog("open");
}

// Delete User
function DeleteUser(id) {
    var data = {};
    data.id = id;
    PostServerData(_deleteUserUrl, data, DeleteUserCallback);
}

function DeleteUserCallback(data) {
    if (data.result) {
        showMessageBox("Xóa thành công", "Thông báo");
        GetUserPage(_currentPage, 20, _currentOrder);
    } else {
        showMessageBox("Không thể xóa.<br />" + data.message, "Thông báo");
    }
    $('#dialog-confirm-delete').dialog("close");
}

// Edit user
var _isAddNewUser = true;

//// Save
function SaveUser() {
    if (_isAddNewUser) {
        AddUser();
    } else {
        SaveEditUser();
    }
}


function SaveEditUser() {
    var user = {};
    user.Id=$('#add_id').val();
    //user.Email = $('#add_email').val();
    user.FirstName = $("#add_firstname").val();
    user.LastName = $("#add_lastname").val();
    user.Phone = $("#add_phone").val();
    user.Address = $("#add_address").val();
    user.Password = SHA256($("#add_password").val());
    user.RoleId = $("#add_role").val();
    user.Status = $("#add_status").val();

    PostServerData(_saveEditUserUrl, user, SaveEditUserCallback);

    return false;
}

function SaveEditUserCallback(data) {
    if (data.result) {
        showMessageBox("Sửa thành công", "Thông báo");
        $('#frm-create-user').dialog("close");
        GetUserPage(_currentPage, 20, _currentOrder);
    } else {
        showMessageBox("Sửa thất bại. <br />" + data.message, "Thông báo");
    }
}

//// Load
function LoadEditUser(id) {
    var data = {};
    data.id = id;
    PostServerData(_loadUserUrl, data, LoadEditUserCallback);
}

function LoadEditUserCallback(data) {
    if (data.result) {
        // Set data to form
        $('#add_id').val(data.User.Id);
        $('#add_email').val(data.User.Email);
        $('#add_firstname').val(data.User.FirstName);
        $('#add_lastname').val(data.User.LastName);
        $('#add_phone').val(data.User.Phone);
        $('#add_address').val(data.User.Address);
        $('#add_password').val("Tt!@#123");
        $('#add_re_password').val("Tt!@#123");
        $('#add_status').val(data.User.Status);
        $('#add_role').val(data.User.Role);

        OpenEditUserForm();
    } else {
        showMessageBox("Không tải được dữ liệu.<br />" + dada.message, "Thông báo");
    }
    $('#overlay-loading').hide();
}

function LoadUserInformation(id) {
    //
}

function LoadUserInformationCallBack(data) {
    //
}

function LoadUserForm() {
    if ($('#frm-create-user').length == 0) {
        GetServerData(_getUserPageUrl, LoadUserFormCallback);
    }
}

function LoadUserFormCallback(data) {
    if (data.result != undefined && !data.result) {
        showMessageBox("Sảy ra lỗi trong quá tringf tải dữ liệu.<br />" + data.message, "Lỗi tải dữ liệu");
    } else {
        $('body').append(data);
    }
}